package com.example;

import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @Description
 * @Author 26233
 * @Create 2021-11-02 11:47:41
 */

public class YamlParser {

    private String fileName;
    private Yaml yaml = new Yaml();
    private Iterator<Object> objects;

    public YamlParser(){}
    public YamlParser(InputStream inputStream){
        objects = yaml.loadAll(inputStream).iterator();
    }

    public YamlParser(String fileName){
        this.fileName = fileName;
    }

    public String getZkAddress(){
        InputStream resourceAsStream = getClass().getResourceAsStream(fileName);
        System.out.println("resourceAsStream = " + resourceAsStream);
        objects = yaml.loadAll(resourceAsStream).iterator();

        String zkAddress = null;
        while (objects.hasNext()){
            Map<String, Object> next = (Map<String, Object>) objects.next();
            Set<Map.Entry<String, Object>> entries = next.entrySet();
            for (Map.Entry<String, Object> e : entries){
                if ("zk".equals(e.getKey())){
                    System.out.println("e.getKey() = " + e.getKey());
                    Map<String, Object> v = (Map<String, Object>) e.getValue();
                    boolean address = v.containsKey("address");
                    System.out.println("address = " + address);
                    zkAddress = (String) v.get("address");
                    break;
                }
            }
            if (zkAddress == null){
                throw new RuntimeException("no zk address in config.");
            }
        }
        return zkAddress;
    }

    public String getServerHost(){
        InputStream resourceAsStream = getClass().getResourceAsStream(fileName);
        System.out.println("resourceAsStream = " + resourceAsStream);
        objects = yaml.loadAll(resourceAsStream).iterator();
        String serverHost = null;
        while (objects.hasNext()){
            Map<String, Object> next = (Map<String, Object>) objects.next();
            Set<Map.Entry<String, Object>> entries = next.entrySet();
            for (Map.Entry<String, Object> e : entries){
                if ("server".equals(e.getKey())){
                    System.out.println("e.getKey() = " + e.getKey());
                    Map<String, Object> v = (Map<String, Object>) e.getValue();
                    boolean address = v.containsKey("host");
                    System.out.println("host = " + address);
                    serverHost = (String) v.get("host");
                    break;
                }
            }
            if (serverHost == null){
                throw new RuntimeException("no server host in config.");
            }
        }
        return serverHost;
    }

    public int getServerPort(){
        InputStream resourceAsStream = getClass().getResourceAsStream(fileName);
        System.out.println("resourceAsStream = " + resourceAsStream);
        objects = yaml.loadAll(resourceAsStream).iterator();
        int serverPort = -1;
        while (objects.hasNext()){
            Map<String, Object> next = (Map<String, Object>) objects.next();
            Set<Map.Entry<String, Object>> entries = next.entrySet();
            for (Map.Entry<String, Object> e : entries){
                if ("server".equals(e.getKey())){
                    System.out.println("e.getKey() = " + e.getKey());
                    Map<String, Object> v = (Map<String, Object>) e.getValue();
                    boolean address = v.containsKey("port");
                    System.out.println("port = " + address);
                    serverPort = (Integer) v.get("port");
                    break;
                }
            }
            if (serverPort < 0){
                throw new RuntimeException("no server host in config.");
            }
        }
        return serverPort;
    }

    public static void main(String[] args) {

        YamlParser yamlParser = new YamlParser("/config.yaml");
        String zkAddress = yamlParser.getZkAddress();
        System.out.println("zkAddress = " + zkAddress);

    }

}
